﻿using Kdbndp;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilZ.Dotnet.DBIBase;

namespace UtilZ.Dotnet.KingbaseES
{
    internal class KingbaseESDBInteraction : DBInteractionAbs
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public KingbaseESDBInteraction()
            : base()
        {

        }

        private const string PARASIGN = ":";
        /// <summary>
        /// 数据库参数字符
        /// </summary>
        public override string ParaSign
        {
            get { return PARASIGN; }
        }

        /// <summary>
        /// 创建数据库拼接连接字符串
        /// </summary>
        /// <param name="config">数据库配置</param>
        /// <param name="visitType">数据库访问类型</param>
        /// <returns>数据库连接字符串</returns>
        protected override string CreateDBSpliceConStr(DatabaseConfig config, DBVisitType visitType)
        {
            if (config.Port <= 0)
            {
                config.Port = 54321;
            }

            //"Server=127.0.0.1;User Id=SYSTEM;Password = MANAGER; Database = TEST; Port = 54321";
            //return $"Server={config.Host};User Id={config.Account};Password = {config.Password}; Database = {config.DatabaseName}; Port = {config.Port}";

            var builder = new KdbndpConnectionStringBuilder();
            builder.Host = config.Host;
            builder.Port = config.Port;
            builder.Database = config.DatabaseName;
            builder.Username = config.Account;
            builder.Password = config.Password;
            return builder.ConnectionString;
        }

        /// <summary>
        /// 获取DbProviderFactory
        /// </summary>
        /// <returns>DbProviderFactory</returns>
        public override DbProviderFactory GetProviderFactory()
        {
            return KdbndpFactory.Instance;
        }

        /// <summary>
        /// 转换原始查询SQL语句为分页查询SQL语句
        /// </summary>
        /// <param name="sqlStr">原始查询SQL语句</param>
        /// <param name="orderStr">排序字符串</param>
        /// <param name="pageIndex">目标页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="dataBaseVersion">数据库版本号信息</param>
        /// <param name="pagingAssistFieldName">分页字段名称</param>
        /// <returns>分页查询SQL语句</returns>
        protected override string ConvertSqlToPagingQuerySql(string sqlStr, string orderStr, long pageIndex, long pageSize, DataBaseVersionInfo dataBaseVersion, out string pagingAssistFieldName)
        {
            /**************************************************************************************
             * 第一种,100就是你需要多少行；
             * SELECT * FROM test_table WHERE i_id>1000 order by i_id  limit 100;
             * 
             * 第二种
             * SELECT * FROM test_table order by i_id limit 100 OFFSET 1000;
             * 1000就是查询的起点位置,也就是从起点1000开始查询，返回100条数据。
             * 
             **************************************************************************************/

            //dataBaseVersion:3.8.2
            //eg:SELECT * from person WHERE ID < 100 ORDER by ID DESC limit 0,10
            pagingAssistFieldName = null;
            var startIndex = (pageIndex - 1) * pageSize;
            string dstSqlStr = null;
            if (string.IsNullOrWhiteSpace(orderStr))
            {
                dstSqlStr = $@"{sqlStr} limit {startIndex} OFFSET {pageSize}";
            }
            else
            {
                dstSqlStr = $@"{sqlStr} ORDER BY {orderStr} limit {startIndex} OFFSET {pageSize}";
            }

            return dstSqlStr;
        }

        private readonly KingbaseESSqlFieldValueFormator _sqlFieldValueFormator = new KingbaseESSqlFieldValueFormator();
        /// <summary>
        /// 获取sql字段值格式化对象
        /// </summary>
        /// <returns>sql字段值格式化对象</returns>
        protected override ISqlFieldValueFormator GetSqlFieldValueFormator()
        {
            return this._sqlFieldValueFormator;
        }
    }
}
